// const oBtn = $('button')
// 写入验证码
$('[id="sc"]').html(mySetVc());
//点击切换验证码
$('[id="sc"]').click(function () {
  $('[id="sc"]').html(mySetVc());
})
$('[name="name"]').change(function(){
  // console.log(111)
  isName();
  
})
  



$('button').click(function (e) {
let username = $('[name="name"]').val();
let userpwd = $('[name="pwd1"]').val();
let pwd = $('[name="pwd2"]').val();
let vc = $('[name="vc"]').val();
  if($(e.target).attr('name') === "register"){
// 获取公共变量


// 验证码是否一致
// toLowerCase()统一小写
$('[id="sc"]').html().toLowerCase()
if (vc.toLowerCase() !== $('#sc').html().toLowerCase()) {
  // 如果验证码不同 输入提示信息
  $('[name="vcSpan"]').html('验证码有误！').css( {color: 'red'});
  // 刷新验证码
  $('[id="sc"]').html(mySetVc());
  return;
} else {
  // 验证码相同 清除 错误提示信息
  $('[name="vcSpan"]').html('');
}
// 两次密码是否一致
if(userpwd !== pwd ){
  // 写入提示
$('[name = "pwdVc"]').html('密码不一致！').css( {color: 'red'});
  // 刷新验证码
  $('[id="sc"]').html(mySetVc());
  return;
}else{
  $('[name = "pwdVc"]').html('');
}


register(username,userpwd);
}else if($(e.target).attr('name') === "reset"){
    console.log(111);
$('[name="name"]').val('');
$('[name="pwd1"]').val('');
$('[name="pwd2"]').val('');
$('[name="vc"]').val('');
  }
})

// ajax请求
async function register(username,userpwd){
  const response = await myPromiseJQueryAjax('../server/goods_register.php','post',{userName:username,userPwd:userpwd},'json')
  console.log(response);

  let num = 5;
  $('[name = "msg"]').html(`注册成功,${num}秒以后跳转至登录界面，点击跳转<a href = "../pages/index.html">首页</a>`)
  let time =setInterval(function(){
    num--;
    $('[name = "msg"]').html(`注册成功,${num}秒以后跳转至登录界面，点击跳转<a href = "../pages/index.html">首页</a>`);
    if(num === 1){
      clearInterval(time);
      window.location.href = `../pages/login.html?id=${response.id}`;
    }
  },1000)
}
async function isName(){
  let username = $('[name="name"]').val();
  const p = await myPromiseJQueryAjax('../server/goods_select_name.php','post',{userName:username},'json');
  if(p.code === 0){
    $('#xx').html('账号重复!').css({color:'red'});
  }else if(p.code === 1){
    $('#xx').html('账号正确').css({color:'blue'});
  }
  
}